﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.ThingsWeb.Goods
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGoodsCategory();

                if (Request.Params["ID"] != null && Request.Params["ID"].Trim() != "")
                {
                    Guid productID = Guid.Empty;
                    Guid.TryParse(Request.Params["ID"], out productID);
                    if (productID != Guid.Empty)
                    {                        
                        
                        btnGoCrops.Visible = true;
                    }
                }
                else
                {
                    
                    btnGoCrops.Visible = false;
                }
            }
        }

        protected void BindGoodsCategory()
        {
            Things.BLL.GoodsCategory bll = new BLL.GoodsCategory();
            DataSet ds = bll.GetList(" IsDeleted=0 ");

            ddlGoodsCategory.Items.Clear();
            ddlGoodsCategory.Items.Add(new ListItem(" -- 请选择 -- ", "-1"));

            Things.Common.UICommon.BindDDLTree(ddlGoodsCategory, ds.Tables[0], Guid.Empty, "&nbsp;&nbsp;&nbsp;");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            Guid productID = Guid.Empty;
            Guid.TryParse(Request.Params["ID"], out productID);
            if (productID == Guid.Empty)
            {
                strErr += "产品信息有误！\\n";
            }
            if (this.txtShelfLife.Text.Trim().Length == 0)
            {
                strErr += "保质期不能为空！\\n";
            }
            if (this.txtType.Text.Trim().Length == 0)
            {
                strErr += "规格不能为空！\\n";
            }
           
            if (!PageValidate.IsNumber(txtCount.Text))
            {
                strErr += "数量格式错误！\\n";
            }
            if (this.txtGoodsName.Text.Trim().Length == 0)
            {
                strErr += "商品名称不能为空！\\n";
            }
            if (this.txtGoodsBrand.Text.Trim().Length == 0)
            {
                strErr += "商品品牌不能为空！\\n";
            }
            if (this.txtGoodsWeight.Text.Trim().Length == 0)
            {
                strErr += "商品重量不能为空！\\n";
            }
            if (this.txtGoodsPackage.Text.Trim().Length == 0)
            {
                strErr += "商品包装不能为空！\\n";
            }
            if (this.txtShengChanPiCi.Text.Trim().Length == 0)
            {
                strErr += "生产批次不能为空！\\n";
            }

            byte[] content = Things.Common.UICommon.GetImages(FileUpload1);
            if (string.IsNullOrEmpty(FileUpload1.FileName) == false && content.Length <= 0)
            {
                strErr += "图片格式不正确！\\n";
            }
            string GoodsImageURL = UploadFile(FileUpload1);
            if (string.IsNullOrEmpty(GoodsImageURL) == true)
            {
                strErr += "图片上传路径不正确！\\n";
            }

            Guid goodsCategoryID = Guid.Empty;
            if (this.ddlGoodsCategory.SelectedValue!="-1" &&  Guid.TryParse(this.ddlGoodsCategory.SelectedValue, out goodsCategoryID) == false)
            {
                strErr += "商品分类有误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string ShelfLife = this.txtShelfLife.Text;
            string Type = this.txtType.Text;
            int Count = int.Parse(this.txtCount.Text);
            bool IsDeleted = false;
            string GoodsName = this.txtGoodsName.Text;
            string GoodsBrand = this.txtGoodsBrand.Text;
            string GoodsWeight = this.txtGoodsWeight.Text;
            string GoodsPackage = this.txtGoodsPackage.Text;
            byte[] GoodsImage = content;

            Things.Model.Goods model = new Things.Model.Goods();
            model.ProductID = productID;
            model.GoodsCategoryID = goodsCategoryID;
            model.ShelfLife = ShelfLife;
            model.Type = Type;
            model.Count = Count;
            model.IsDeleted = IsDeleted;
            model.GoodsName = GoodsName;
            model.GoodsBrand = GoodsBrand;
            model.GoodsWeight = GoodsWeight;
            model.GoodsPackage = GoodsPackage;
            model.GoodsImage = GoodsImage;
            model.GoodsImageURL = GoodsImageURL;
            model.ShengChanPiCi = this.txtShengChanPiCi.Text.Trim();
            model.ChuCangFangFa = this.txtChuChangFangFa.Text.Trim();
            model.ShengChanXuKeZhengBianHao = this.txtShengChanXuKeZhengBianHao.Text.Trim();
            model.ChanPinBiaoZhunHao = this.txtChanPinBiaoZhunHao.Text.Trim();

            Things.BLL.Goods bll = new Things.BLL.Goods();
            bll.Add(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");
            
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        protected void btnGoCrops_Click(object sender, EventArgs e)
        {
            Response.Redirect("../CropsProduct/list.aspx");
        }

        public string UploadFile(FileUpload FileUpload1)
        {
            string imageURL = string.Empty;
            try
            {
                string filePath = FileUpload1.PostedFile.FileName;
                string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                string serverPath = Server.MapPath("~/images/") + fileName;
                FileUpload1.PostedFile.SaveAs(serverPath);
                imageURL = "/images/" + fileName;
            }
            catch
            {
                imageURL = string.Empty;
            }

            return imageURL;
        }
    }
}
